﻿using OVS.Core;
using OVS.Domain;
using OVS.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ServiceModel;
using System.ServiceModel.Web;
using NHibernate.Criterion;

namespace OVS.webUI.Controllers
{
    public class MatchController : Controller
    {
        // GET: Match
        /// <summary>
        /// 
        /// </summary>
        /// <param name="matchChoose">场次ID</param>
        /// <param name="player1Add">选手1ID</param>
        /// <param name="player2Add">选手2ID</param> 
        /// <param name="player1Song">选手1歌曲名</param>
        /// <param name="player2Song">选手2歌曲名</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Create(int matchChoose, int player1Add, int player2Add, string player1Song, string player2Song)
        {
            MatchPKInfo mpi = new MatchPKInfo();
            mpi.FirstPlayer = new Player() { ID = player1Add };
            mpi.Matchs = new Matchs() { ID = matchChoose };
            mpi.SecondPlayer = new Player() { ID = player2Add };
            mpi.FirstSongName = player1Song.ToString();
            mpi.SecondSongName = player2Song.ToString() ;
            mpi.BattleFlag = 0;

            Container.Instance.Resolve<IMatchPKInfoService>().Create(mpi);

            return Json(1);
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Type">不同的信息：eg：1-Player;2--Match;3MatchPKInfo</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetMatchInfo(int Type)
        {
            switch(Type)
            {
                //返回Players
                case 1:
                    IList<Player> iPlayers = Container.Instance.Resolve<IPlayerService>().GetAll();
                    return Json(iPlayers, JsonRequestBehavior.AllowGet);
                    //返回Matchs
                case 2:
                    IList<Matchs> iMatchs = Container.Instance.Resolve<IMatchService>().GetAll();
                    return Json(iMatchs, JsonRequestBehavior.AllowGet);
                    //返回MatchPKInfo
                case 3:
                    IList<MatchPKInfo> imp = Container.Instance.Resolve<IMatchPKInfoService>().GetAll();
                    return Json(imp, JsonRequestBehavior.AllowGet);
                default:return Json(0,JsonRequestBehavior.AllowGet);
            }
        }

        //更改状态   开始--停止
        [HttpPost]
        public JsonResult MatchChange(int ID)
        {
            MatchPKInfo mpi = Container.Instance.Resolve<IMatchPKInfoService>().Get(ID);
            if (mpi.BattleFlag == 0 || mpi.BattleFlag == 2)
            {
                //nHibanate
                IList<ICriterion> queryConditions = new List<ICriterion>();
                queryConditions.Add(Expression.Eq("BattleFlag", 1));
                IList<MatchPKInfo> mpI = Container.Instance.Resolve<IMatchPKInfoService>().Find(queryConditions);
                foreach (var item in mpI)
                {
                    item.BattleFlag = 3;
                    Container.Instance.Resolve<IMatchPKInfoService>().Update(item);
                }

                mpi.BattleFlag = 1;
            }
            else if (mpi.BattleFlag == 1)
            {
                mpi.BattleFlag = 3;
            }
            Container.Instance.Resolve<IMatchPKInfoService>().Update(mpi);
            return Json(1,JsonRequestBehavior.AllowGet);
        }

        //设置状态为暂停
        [HttpPost]
        public JsonResult StatePause(int ID)
        {
            MatchPKInfo mpi = Container.Instance.Resolve<IMatchPKInfoService>().Get(ID);
            if (mpi.BattleFlag == 1)
            {
                mpi.BattleFlag = 2;
                Container.Instance.Resolve<IMatchPKInfoService>().Update(mpi);
                return Json(1,JsonRequestBehavior.AllowGet);
            }
            return Json(0,JsonRequestBehavior.AllowGet);
        }

        //添加比赛信息
        public JsonResult MatchAdd(string MatchNO,string MatchName,string MatchAddress,string MatchTime,string MatchInfo)
        {
            try
            {
                Matchs ms = new Matchs();
                ms.MatchNO = MatchNO;
                ms.MatchName = MatchName;
                ms.MatchAddress = MatchAddress;
                ms.MatchTime = DateTime.Parse(MatchTime);
                ms.MatchInfo = MatchInfo;
                Container.Instance.Resolve<IMatchService>().Create(ms);
            }
            catch
            {
                return Json(0, JsonRequestBehavior.AllowGet);
            }
            return Json(1, JsonRequestBehavior.AllowGet);
        }
    }
}